package com.chen.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * @Title:多数据源配置
 * @Description: 对配置文件的读取
 * @Author: chenjiwei
 * @Date: 2020-10-30 23:38
 * @Version: 1.0.0
 */
@Configuration
public class DataSourceConfig {
    @Bean(name = "test2DataSource")
    @Qualifier("test2DataSource")
    @ConfigurationProperties(prefix = "test2.datasource")
    public DataSource test2DataSource(){

        return DataSourceBuilder.create().build();
    }

    @Bean(name = "test2JpaProperties")
    @Qualifier("test2JpaProperties")
    @ConfigurationProperties(prefix = "test2.jpa")
    public JpaProperties test2JpaProperties(){
        return new JpaProperties();
    }

    @Bean(name = "test2JdbceTmplate")
    public JdbcTemplate test2JdbcTemplate(@Qualifier("test2DataSource") DataSource dataSource)
    {

        return new JdbcTemplate(dataSource);

    }

    @Primary
    @Bean(name = "testDataSource")
    @Qualifier("testDataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource testDataSource(){

        return DataSourceBuilder.create().build();
    }

    @Bean(name = "testJpaProperties")
    @Primary
    @Qualifier("testJpaProperties")
    @ConfigurationProperties(prefix = "spring.jpa")
    public JpaProperties testJpaProperties(){
        return new JpaProperties();
    }

    @Bean(name = "testJdbcTemplate")
    public JdbcTemplate testJdbcTemplate(@Qualifier("testDataSource") DataSource dataSource){

        return new JdbcTemplate(dataSource);

    }
}
